package com.ls.controller.a;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.Page;
import com.ls.common.ActionEnum;
import com.ls.common.BaseController;
import com.ls.common.entity.JsonResult;
import com.ls.common.entity.Tree;
import com.ls.model.Permission;
import com.ls.service.PermissionService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

import static com.ls.common.Const.*;

/**
 * <p></p>
 * Created by zhezhiyong@163.com on 2017/2/9.
 */
@RestController
@RequestMapping("/permission")
public class PermissionController extends BaseController {

    @Resource
    private PermissionService permissionService;

    @GetMapping(value = "/tree")
    public List<Tree> tree() {
        return permissionService.treeList(null);
    }

    @GetMapping(value = "/tree/{roleId}")
    public List<Tree> treeCheck(@PathVariable int roleId) {
        return permissionService.treeList(roleId);
    }

    //    @RequiresPermissions("permission:view")
    @RequestMapping("/list")
    public JSONObject list(@RequestParam(required = false) Integer page, @RequestParam(required = false) Integer rows) {
        JSONObject object = new JSONObject();
        List<Permission> permissionList = permissionService.list(page, rows);
        object.put("rows", permissionList);
        object.put("total", ((Page) permissionList).getTotal());
        return object;
    }

    @RequestMapping(value = "/{id}")
    public Permission get(@PathVariable Integer id) {
        return permissionService.getById(id);
    }

    //    @RequiresPermissions("permission:save")
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public JsonResult save(HttpServletRequest request, Permission permission) {
        int _parentId = Integer.parseInt(request.getParameter("_parentId"));
        permission.set_parentId(_parentId);
        permission.setUpdateTime(new Date());
        Permission parent = permissionService.getById(_parentId);
        permission.setParentIds(parent.getParentIds() + parent.getId() + "/");
        permissionService.save(permission);
        return JsonResult.SUCCESS;
    }

    //    @RequiresPermissions("permission:update")
    @RequestMapping(value = "/update/{id}", method = RequestMethod.POST)
    public JsonResult update(@PathVariable int id, Permission permission) {
        permission.setId(id);
        permissionService.update(permission);
        return JsonResult.SUCCESS;
    }

    //    @RequiresPermissions("permission:delete")
    @RequestMapping(value = "/delete/{id}")
    public JsonResult delete(@PathVariable Integer id) {
        Permission p = permissionService.getById(id);
        if (p.getType() == TABLE_PERMISSION_TYPE_MENU) {
            permissionService.deleteByParentId(id);
        }
        if (p.getType() != TABLE_PERMISSION_TYPE_SYSTEM) {
            permissionService.delete(id);
        }
        return JsonResult.SUCCESS;
    }

    @PostMapping(value = "/view/save")
    public JsonResult viewSave(HttpServletRequest request) {
        String name = request.getParameter(KEY_PERMISSION_NAME);
        String view = request.getParameter(KEY_PERMISSION_VIEW);
        List<String> actionList = Arrays.asList(request.getParameterValues(KEY_PERMISSION_ACTION));
        Permission permission = new Permission(name, 2, "/" + view, "", 1, "0/1/", view, 1, new Date(), "");
        permissionService.save(permission);
        Integer pid = permission.getId();
        actionList.forEach(action -> {
            ActionEnum ae = ActionEnum.getName(action);
            if (ae != null && action.equals(ae.getPermission()) && pid != null) {
                permissionService.save(new Permission(ae.getName(), 3, "/" + view + "/" + action, ae.getIconCls(), pid, "0/1/" + pid + "/", view + ":" + action, 1, new Date(), ""));
            }
        });
        return JsonResult.SUCCESS;
    }

}
